<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\Request;
use App\Http\Middleware\SystemStatisticsMiddleware;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use App\Http\Middleware\StratorAuthMiddleware;

//api routes
$apiRoutes = [
    __DIR__.'/../routes/api.php',
    __DIR__.'/../routes/strator.php',
];

//web routes
$webRoutes = [
    __DIR__.'/../routes/web.php',
];

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: $webRoutes,
        api: $apiRoutes,
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        //global
        $middleware->append([
            SystemStatisticsMiddleware::class,
        ]);

        //
        $middleware->alias([
            //login check
            'strator.auth' => StratorAuthMiddleware::class
        ]);

    })
    ->withExceptions(function (Exceptions $exceptions) {
        //404
        $exceptions->render(function (NotFoundHttpException $e, Request $request) {
            return response()->json([
                'status'=>'fail',
                'msg'=>$e->getMessage(),
                'error'=>'未查到对应数据，请核对参数',
            ], 201);
        });

    })->create();
